package com.mdd.admin.service.session.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.session.IAdminSessionService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.session.AdminSessionParam;
import com.mdd.admin.vo.session.AdminSessionDetailVo;
import com.mdd.admin.vo.session.AdminSessionListVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.session.AdminSession;
import com.mdd.common.mapper.session.AdminSessionMapper;
import com.mdd.common.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 会话实现类
 */
@Service
public class AdminSessionServiceImpl implements IAdminSessionService {
        
    @Resource
    AdminSessionMapper adminSessionMapper;

    /**
     * 会话列表
     *
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<AdminSessionListVo>
     */
    @Override
    public PageResult<AdminSessionListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        QueryWrapper<AdminSession> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        adminSessionMapper.setSearch(queryWrapper, params, new String[]{
            "=:adminId@admin_id:long",
            "=:terminal:int",
            "=:token:str",
            "=:expireTime@expire_time:int",
        });

        IPage<AdminSession> iPage = adminSessionMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<AdminSessionListVo> list = new LinkedList<>();
        for(AdminSession item : iPage.getRecords()) {
            AdminSessionListVo vo = new AdminSessionListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 会话详情
     *
     * @param id 主键参数
     * @return AdminSession
     */
    @Override
    public AdminSessionDetailVo detail(Integer id) {
        AdminSession model = adminSessionMapper.selectOne(
                new QueryWrapper<AdminSession>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        AdminSessionDetailVo vo = new AdminSessionDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 会话新增
     *
     * @param adminSessionParam 参数
     */
    @Override
    public void add(AdminSessionParam adminSessionParam) {
        AdminSession model = new AdminSession();
        model.setAdminId(adminSessionParam.getAdminId());
        model.setTerminal(adminSessionParam.getTerminal());
        model.setToken(adminSessionParam.getToken());
        model.setExpireTime(adminSessionParam.getExpireTime());
        adminSessionMapper.insert(model);
    }

    /**
     * 会话编辑
     *
     * @param adminSessionParam 参数
     */
    @Override
    public void edit(AdminSessionParam adminSessionParam) {
        AdminSession model = adminSessionMapper.selectOne(
                new QueryWrapper<AdminSession>()
                    .eq("id",  adminSessionParam.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(adminSessionParam.getId());
        model.setAdminId(adminSessionParam.getAdminId());
        model.setTerminal(adminSessionParam.getTerminal());
        model.setToken(adminSessionParam.getToken());
        model.setExpireTime(adminSessionParam.getExpireTime());
        adminSessionMapper.updateById(model);
    }

    /**
     * 会话删除
     *
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        AdminSession model = adminSessionMapper.selectOne(
                new QueryWrapper<AdminSession>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        adminSessionMapper.delete(new QueryWrapper<AdminSession>().eq("id", id));
    }

}
